<?php

use console\db\Migration;
use yii\db\Query;
use yii\helpers\ArrayHelper;
use yii\helpers\Console;

/**
 * 用户转移准备
 * 
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class m300000_000000_prepare_trans_user extends Migration {

    /**
     * @inheritdoc
     */
    public function safeUp() {

        /**
         * 首先复制用户表到新数据库
         * @create table `sns_dynamic_comment`.
         */
        $requireTables = $this->requiredTransferTable();
        if (!empty($requireTables)) {
            foreach ($requireTables as $table) {
                if (!$this->isExistsTable($table)) {
                    Console::error('Please migrate the original table: ' . $table . ' using the database tool first.');
                    return false;
                }
            }
        }

        /**
         * 创建升级所需的临时表
         */
        foreach ($this->tempTable() as $table) {
            $this->tableBuilder($table);
        }

        /**
         * 升级所需增加的表字段
         */
        foreach ($this->tempAddColumns() as $table => $columns) {
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            $tableColumns = $this->db->getTableSchema($tableName)->columnNames;
            if (empty($columns)) {
                continue;
            }
            foreach ($columns as $column => $type) {
                if (!in_array($column, $tableColumns)) {
                    $this->addColumn($tableName, $column, $type);
                } else {
                    $this->alterColumn($tableName, $column, $type);
                }
            }
        }

        /**
         * 升级所需增加的表索引
         */
        foreach ($this->tempAddIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->createIndex($index, $tableName, $index);
            }
        }

        /**
         * 升级所需临时移除的表索引
         */
        foreach ($this->tempRemoveIndexs() as $table => $indexs) {
            if (empty($indexs)) {
                continue;
            }
            $tableName = '{{%' . $this->basenameTable($table) . '}}';
            foreach ($indexs as $index) {
                $this->dropIndex($index, $tableName);
            }
        }
    }

    /**
     * @inheritdoc
     */
    public function safeDown() {
        
    }

    /**
     * 迁移前所需的数据表
     * @todo 复制所需数据表至新数据库
     * @return array
     */
    public function requiredTransferTable() {
        return [
            '{{%member}}'
        ];
    }

    /**
     * 升级所需的临时表
     * @return array
     */
    public function tempTable() {
        return [];
    }

    /**
     * 升级所需增加的表字段
     * @return array
     */
    public function tempAddColumns() {
        return [
            'user' => [
                'temp_mobile' => $this->char(32)->after('mobile'),
            ],
            'user_meta' => [
                'temp_mobile' => $this->char(255)->after('mobile'),
                'temp_ip' => $this->char(32)->after('mobile'),
            ],
        ];
    }

    /**
     * 升级所需增加的临时索引
     * @return array
     */
    public function tempAddIndexs() {
        return [
            'user' => [
                'temp_mobile'
            ],
            'user_meta' => [
                'temp_mobile',
                'temp_ip'
            ],
        ];
    }

    /**
     * 升级所需临时移除的索引
     * @return array
     */
    public function tempRemoveIndexs() {
        return [
            'user_authent' => [
                'idcardno'
            ]
        ];
    }

}
